package com.bwf.lgqingdan.mvp.model.impl;

import com.alibaba.fastjson.JSON;
import com.bwf.lgqingdan.entity.ResponseArticles;
import com.bwf.lgqingdan.mvp.model.ArticleListModel;
import com.bwf.lgqingdan.utils.http.lib.HttpUtils;
import com.bwf.lgqingdan.utils.http.lib.Request;
import com.bwf.lgqingdan.utils.http.qingdan.HttpClient;

/**
 * Created by Administrator on 2016/11/18.
 */

public class ArticleListModelImpl implements ArticleListModel{
    @Override
    public void loadArticles(String url, final Callback callback) {
        Request.Builder builder = new Request.Builder()
                .url(url)
                .get();
        HttpClient.excute(builder, new HttpUtils.Callback() {
            @Override
            public void onResponse(String response) {
                ResponseArticles responseArticles = JSON.parseObject(response, ResponseArticles.class);
                if(responseArticles.getCode() == 0){
                    callback.loadArticlesSuccess(responseArticles);
                }else{
                    callback.loadArticlesFailed(responseArticles.getMessage());
                }
            }

            @Override
            public void onError() {
                callback.loadArticlesFailed("连接服务器失败");
            }
        });
    }
}
